<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="代理商名称">
              <a-input placeholder="请输入代理商名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="visible = true" type="primary" icon="plus">新增</a-button>
      <a-button v-if="selectedRowKeys.length > 0" type="danger" icon="delete" @click="handleDelete2">删除</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除该条代理商信息吗?" @confirm="() => handleDelete(record.id)">
            <a style="color: red">删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <a-modal
      :title="title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :width="450"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form-model ref="ruleForm" :model="formData" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="名称" prop="name">
          <a-input v-model="formData.name" placeholder="请输入代理商名称" />
        </a-form-model-item>
        <a-form-model-item label="电话" prop="phone">
          <a-input v-model="formData.phone" placeholder="请输入代理商电话" />
        </a-form-model-item>
        <a-form-model-item label="地址" prop="address">
          <a-input v-model="formData.address" placeholder="请输入代理商地址" />
        </a-form-model-item>
        <a-form-model-item label="推荐人" prop="superiorNum">
          <a-select v-model="formData.superiorNum" placeholder="请选择代理商推荐人">
            <a-select-option v-for="item in referenceList" :key="item.id" :value="item.id">{{
              item.realname
            }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { apiGetReferenceList, apiAddAgent, apiEditAgent, apiDelAgent, apiDelAgentBatch } from '@/api/agentManage.js'

export default {
  name: 'AgentManageMaintenance',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      description: '客户信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '代理商名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '电话',
          align: 'center',
          dataIndex: 'phone'
        },
        {
          title: '地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '推荐人',
          align: 'center',
          dataIndex: 'superiorNum'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/gzAgent/list',
        delete: '/gzCustomInfo/delete',
        deleteBatch: '/gzCustomInfo/deleteBatch',
        exportXlsUrl: '/gzCustomInfo/exportXls',
        importExcelUrl: 'gzCustomInfo/importExcel'
      },
      title: '新增代理商信息',
      visible: false,
      confirmLoading: false,
      formData: {
        name: undefined,
        phone: undefined,
        address: undefined,
        superiorNum: undefined
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      rules: {
        name: [{ required: true, message: '请输入代理商名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入代理商地址', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入代理商电话', trigger: 'blur' }],
        superiorNum: [{ required: true, message: '请选择推荐人', trigger: 'change' }]
      },
      referenceList: []
    }
  },
  created() {
    this.gcetReferenceList()
  },
  methods: {
    async gcetReferenceList() {
      this.referenceList = (await apiGetReferenceList()).result
    },
    handleCancel() {
      this.$refs.ruleForm.resetFields()
      this.visible = false
    },
    async handleOk() {
      await this.$refs.ruleForm.validate()
      const apiFun = this.title === '新增代理商信息' ? apiAddAgent : apiEditAgent
      this.confirmLoading = true
      const { code, message } = await apiFun(this.formData)
      if (code === 200) {
        this.$message.success(message)
        this.handleCancel()
        this.searchQuery()
      } else {
        this.$message.error(message)
      }
      this.confirmLoading = false
    },
    // 编辑按钮
    handleEdit(row) {
      this.title = '编辑代理商信息'
      this.visible = true
      this.formData = Object.assign({}, row)
    },
    // 行内删除按钮
    async handleDelete(id) {
      const { code, message } = await apiDelAgent(id)
      if (code === 200) {
        this.$message.success(message)
        this.searchQuery()
      } else {
        this.$message.error(message)
      }
    },
    handleDelete2() {
      this.$confirm({
        title: '删除提醒',
        content: '确认删除所选代理商数据嘛？',
        onOk: async () => {
          const { code, message } = await apiDelAgentBatch(this.selectedRowKeys.join(','))
          if (code === 200) {
            this.$message.success(message)
            this.searchQuery()
          } else {
            this.$message.error(message)
          }
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
